package com.fh.service.impl;

import com.fh.mapper.ProductCommentsMapper;
import com.fh.mapper.ProductParamsMapper;
import com.fh.model.ProductComments;
import com.fh.model.ProductParams;
import com.fh.model.vo.ProductCommentsVO;
import com.fh.service.ProductCommentsService;
import com.fh.service.ProductParamsService;
import com.fh.utils.PageHelper;
import com.fh.utils.ResultState;
import com.fh.vo.ResultVo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {
@Resource
private ProductCommentsMapper productCommentsMapper;


    @Override
    public ResultVo selectComment(String productId,Integer pageNum,Integer limit) {
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        //总条数
        int count = productCommentsMapper.selectCountByExample(example);

        //总页数
        int pageCount=count%limit==0?count/limit:count/limit+1;

        //查询当前页数据
        int start=(pageNum-1)*limit;


        List<ProductCommentsVO> productComments = productCommentsMapper.selectComment(productId,start,limit);

        return new ResultVo(ResultState.OK,"ok",new PageHelper<ProductCommentsVO>(count,pageCount,productComments));
    }

    @Override
    public ResultVo selectCommentType(String productId) {
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        //评论总条数
        int count = productCommentsMapper.selectCountByExample(example);
        //好评
        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("productId",productId);
        criteria2.andEqualTo("commType",1);
        int goodCount = productCommentsMapper.selectCountByExample(example2);
        //中评
        Example example3 = new Example(ProductComments.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("productId",productId);
        criteria3.andEqualTo("commType",0);
        int zhongCount = productCommentsMapper.selectCountByExample(example3);
        //差评
        Example example4 = new Example(ProductComments.class);
        Example.Criteria criteria4 = example4.createCriteria();
        criteria4.andEqualTo("productId",productId);
        criteria4.andEqualTo("commType",-1);
        int noCount = productCommentsMapper.selectCountByExample(example4);
        //好评率
       double percont=Double.parseDouble(String.valueOf(goodCount))/Double.parseDouble(String.valueOf(count))*100;
       long round=Math.round(percont);
        Map<String,Object> map=new HashMap<>();
        map.put("count",count);
        map.put("goodCount",goodCount);
        map.put("zhongCount",zhongCount);
        map.put("noCount",noCount);
        map.put("round",round);
        return new ResultVo(ResultState.OK,"ok",map);
    }
}
